﻿using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using WPF项目.Common.Models;
using WPF项目.Extensions;

namespace WPF项目.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public MainViewModel(IRegionManager regionManager)
        {
            Menus = new ObservableCollection<NavigationMenu>();
            CreateMenu();

            NavigateCommand = new DelegateCommand<NavigationMenu>(Navigate);

            GoBackCommand = new DelegateCommand(() => {
                if (journal != null && journal.CanGoBack)
                    journal.GoBack();
            });
            GoForwardCommand = new DelegateCommand(() => {
                if (journal != null && journal.CanGoForward)
                    journal.GoForward();
            });
           this.regionManager = regionManager;
        }
        private IRegionManager regionManager;
        private IRegionNavigationJournal journal;
        private void Navigate(NavigationMenu menu)
        {
            if (menu == null || string.IsNullOrWhiteSpace(menu.NamesPace))//string.IsNullOrWhiteSpace判断字段是否为null
                return;

            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(menu.NamesPace, back =>
            {
                journal = back.Context.NavigationService.Journal;
            });
        }

        private ObservableCollection<NavigationMenu> menus;
        public DelegateCommand GoBackCommand { get; private set; }//上一步
        public DelegateCommand GoForwardCommand { get; private set; }//下一步


        public ObservableCollection<NavigationMenu> Menus
        {
            get { return menus; }
            set
            {
                menus = value;
                RaisePropertyChanged();
            }
        }
        // 导航菜单驱动命令
        public DelegateCommand<NavigationMenu> NavigateCommand { get; private set; }


        void CreateMenu()
        {
            Menus.Add(new NavigationMenu() { Icon = "Home", Title = "首页", NamesPace = "HomeViwe" });
            Menus.Add(new NavigationMenu() { Icon = "CalendarTextOutline", Title = "代办事项", NamesPace = "AgentMattersViwe" });
            Menus.Add(new NavigationMenu() { Icon = "Notebook", Title = "备忘录", NamesPace = "MemoViwe" });
            Menus.Add(new NavigationMenu() { Icon = "CogStop", Title = "设置", NamesPace = "InstallViwe" });

        }
    }
}
